package iso
{
	import asset.AssetBase;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;

	public class IsoScene extends IsoSprite
	{
		protected var _assetLayer:IsoSprite;//地图资源层
		protected var _assetFloorLayer:IsoSprite;//地图地表资源层
		protected var _assetArray:Array=[];
		protected var _assetFloorArray:Array=[];
		private var _isAutoDepthSort:Boolean;
		private var _isDrawFloor:Boolean;
		private var _bmFloor:Bitmap;//地表层并合的bitmap
		private var _bmdFloor:BitmapData;//地表层并合的BitmapData
		
		/**
		 * Iso场景基类
		 * @param isAutoDepthSort 是否自动深度排序
		 * @param isDrawFloor 是否合并且绘画地表层
		 */
		public function IsoScene(isAutoDepthSort:Boolean=true, isDrawFloor:Boolean=false)
		{
			_isAutoDepthSort=isAutoDepthSort;
			_isDrawFloor=isDrawFloor;
			
			_assetFloorLayer=new IsoSprite();
			if(!_isDrawFloor) addChild(_assetFloorLayer);
			
			_assetLayer=new IsoSprite();
			addChild(_assetLayer);
		}
		
		/**
		 *增加至地图物件层
		 */		
		public function addToAsset(assetBase:AssetBase):void
		{
			_assetLayer.addChild(assetBase);
			_assetArray.push(assetBase);
			if(_isAutoDepthSort) depthSort();
		}
		
		/**
		 *增加至地表物件层
		 */		
		public function addToFloor(assetBase:AssetBase):void
		{
			_assetFloorLayer.addChild(assetBase);
			_assetFloorArray.push(assetBase);
			if(_isAutoDepthSort) depthSort();
		}
		
		/**
		 *移动至地图物件层
		 */		
		public function moveToAsset(assetBase:AssetBase):void
		{
			if(_assetFloorArray.indexOf(assetBase)!=-1) _assetFloorArray.splice(_assetFloorArray.indexOf(assetBase), 1);
			if(_assetArray.indexOf(assetBase)==-1)
			{
				_assetArray.push(assetBase);
				_assetLayer.addChild(assetBase);
			}
			if(_isAutoDepthSort) depthSort();
		}
		
		/**
		 *移动至地表物件层
		 */		
		public function moveToFloor(assetBase:AssetBase):void
		{
			if(_assetArray.indexOf(assetBase)!=-1) _assetArray.splice(_assetArray.indexOf(assetBase), 1);
			if(_assetFloorArray.indexOf(assetBase)==-1)
			{
				_assetFloorArray.push(assetBase);
				_assetFloorLayer.addChild(assetBase);
			}
			if(_isAutoDepthSort) depthSort();
		}
		
		/**
		 *从地图物件层移除
		 */		
		public function removeAsset(assetBase:AssetBase):void
		{
			if(assetBase)
			{
				if(_assetArray.indexOf(assetBase)!=-1) _assetArray.splice(_assetArray.indexOf(assetBase), 1);
				if(_assetFloorArray.indexOf(assetBase)!=-1) _assetFloorArray.splice(_assetFloorArray.indexOf(assetBase), 1);
				
				if(assetBase.parent) assetBase.parent.removeChild(assetBase);
				assetBase.dispose();
			}
			assetBase=null;
		}
		
		/**
		 *拼合地表层 
		 */		
		public function drawFloor():void
		{
			if(!_isDrawFloor) return;
			
			depthSortFloor();
			
			var mapWidth:Number=Math.max(_assetLayer.width, _assetFloorLayer.width);
			var mapHeight:Number=Math.max(_assetLayer.height, _assetFloorLayer.height);
			
			_bmdFloor=new BitmapData(mapWidth, mapHeight);
			_bmFloor=new Bitmap(_bmdFloor);
			
			_bmdFloor.draw(_assetFloorLayer);
			addChildAt(_bmFloor, 0);
			
			clearAllFloor();
		}
		
		/**
		 *深度排序 
		 */		
		public function depthSort():void
		{
			depthSortAsset();
			
			//当不为拼合地表层时
			if(!_isDrawFloor) depthSortFloor();
		}
		
		/**
		 *深度排序非地表层物件 
		 */		
		private function depthSortAsset():void
		{
			_assetArray.sortOn("depth", Array.NUMERIC);
			for(var i:int = 0; i < _assetArray.length; i++)
			{
				_assetLayer.setChildIndex(_assetArray[i], i);
			}
		}
		
		/**
		 *深度排序地表层物件 
		 */		
		private function depthSortFloor():void
		{
			_assetFloorArray.sortOn("depth", Array.NUMERIC);
			for(var j:int = 0; j < _assetFloorArray.length; j++)
			{
				_assetFloorLayer.setChildIndex(_assetFloorArray[j], j);
			}
		}
		
		/**
		 *清理全部的地图资源物件 
		 */		
		public function clearAll():void
		{		
			clearAllAsset();
			clearAllFloor();
		}
		
		/**
		 *清理非地表层的地图资源物件 
		 */		
		public function clearAllAsset():void
		{
			while(_assetLayer.numChildren>0)
			{
				var mapAssetBase:IsoSprite=_assetLayer.getChildAt(0) as IsoSprite;
				if(mapAssetBase.parent) mapAssetBase.parent.removeChild(mapAssetBase);
				mapAssetBase.dispose();
				mapAssetBase=null;
			}
			
			for(var i:int=0;i<_assetArray.length;i++)
			{
				mapAssetBase=_assetArray[i] as IsoSprite;
				if(mapAssetBase)
				{
					if(mapAssetBase.parent) mapAssetBase.parent.removeChild(mapAssetBase);
					mapAssetBase.dispose();
				}
				mapAssetBase=null;
			}
			_assetArray=[];
		}
		
		/**
		 *清理地表层地图资源物件 
		 */		
		public function clearAllFloor():void
		{		
			while(_assetFloorLayer.numChildren>0)
			{
				var assetBase:AssetBase=_assetFloorLayer.getChildAt(0) as AssetBase;
				if(assetBase.parent) assetBase.parent.removeChild(assetBase);
				assetBase.dispose();
				assetBase=null;
			}
			
			for(var j:int=0;j<_assetFloorArray.length;j++)
			{
				assetBase=_assetFloorArray[j] as AssetBase;
				if(assetBase)
				{
					if(assetBase.parent) assetBase.parent.removeChild(assetBase);
					assetBase.dispose();
				}
				assetBase=null;
			}
			_assetFloorArray=[];
		}
		
		override public function dispose():void
		{
			super.dispose();
			
			clearAll();
			
			if(_bmdFloor) _bmdFloor.dispose();
			_bmdFloor=null;
			
			if(_bmFloor)
			{
				if(_bmFloor.parent) _bmFloor.parent.removeChild(_bmFloor);
				_bmFloor.bitmapData.dispose();
			}
			_bmFloor=null;
			
			if(_assetLayer)
			{
				if(_assetLayer.parent) _assetLayer.parent.removeChild(_assetLayer);
				_assetLayer.dispose();
			}
			_assetLayer=null;
			
			if(_assetFloorLayer)
			{
				if(_assetFloorLayer.parent) _assetFloorLayer.parent.removeChild(_assetFloorLayer);
				_assetFloorLayer.dispose();
			}
			_assetFloorLayer=null;
			
			if(parent) parent.removeChild(this);
		}
	}
}